<?php 
namespace Home\Controller;

use Think\Controller;

//公共的控制器类
class AddressController extends Controller
{


	public function index(){

		$addressM 	= M('address');

		$uid	 	= session('suser_id');


		$data = $addressM->where('uid='.$uid)->select();

		// NSLog($data);


		$this->assign('data',$data);

		$this->display();
	}





	public function  provice()
	{
		$provice = M();
		
	if (isset($_POST['provice']) && $_POST['provice'] == 1)
	{

			

			$data 	= $provice->query("select * from j_position_provice");

			$this->ajaxReturn($data);

			exit();
		}



		if (!empty($_POST['tableName']) && !empty($_POST ['val'])) {

				
			// $sql	= "select * from {$_POST['tableName']} where {$_POST['key']} = {$_POST ['val']}";

			$res	= $provice->query("select * from {$_POST['tableName']} where {$_POST['key']} = {$_POST ['val']}");

			$this->ajaxReturn($res);

				exit();

			}else{

				throw new Exception("Error  Request", 1);
				
				exit();
			}

	}





	public function insert(){

		$provice = M('j_position_provice');
		$city = M('j_position_city');
		$county = M('j_position_county');
		$town = M('j_position_town');
		$village = M('j_position_village');
	

		if (empty($_POST['userName']) || empty($_POST['phone'])||empty($_POST['detail']) || empty($_POST['area'])|| $_POST['provice']=='请选择省份'){
			
			

			$this->ajaxReturn(2);

			exit();

		}


			if (!empty($_POST['provice']) && $_POST['provice']!='请选择省份') {

				$proviceName = $provice->field('provice_name')->where('provice_id='.$_POST['provice'])->find()['provice_name'];
			}


			if (!empty($_POST['city'])) {
				


				$cityName = $city->field('city_name')->where('city_id='.$_POST['city'])->find()['city_name'];
			}




			if (!empty($_POST['county'])) {
				
				$countyName = $county->field('county_name')->where('county_id='.$_POST['county'])->find()['county_name'];
			}


			if (!empty($_POST['town'])) {
				
				$townName = $town->field('town_name')->where('town_id='.$_POST['town'])->find()['town_name'];

				// $townName = $town->field('town_name')->where('town_id=140105001000')->find();
			}


			if (!empty($_POST['village'])) {
				
				$villageName  = $village->field('village_name')->where('village_id='.$_POST['village'])->find()['village_name'];
			}


			$address = $proviceName.$cityName.$countyName.$townName.$villageName;

			$uid	 = session('suser_id');

			// var_dump($_SESSION);

			$data	 = ['area'=>$_POST['area'],'userName'=>$_POST['userName'],'phone'=>$_POST['phone'],'detail'=>$_POST['detail'],'uid'=>$uid,'address'=>$address];





			$addressM = M('address');

			if ($addressM->create($data)) {
				
				if ($addressM->add()>0) {
					
					$this->ajaxReturn(1);
				}else{

					$this->ajaxReturn(0);
				}

			}else{


				echo($addressM->getError());

			}
			
		// var_dump($address); 

		// var_dump($proviceName);
		// var_dump($cityName);
		// var_dump($countyName);
		// var_dump($townName);
		// var_dump($villageName);

	}




	public function del(){


		if (IS_AJAX && !empty($_POST['id'])) {
			
			$addressM 	= M('address');

			if ($addressM->delete($_POST['id'])>0) {
				
				$this->ajaxReturn(true);
			}else{

				$this->ajaxReturn(false);
			}

			
		}else{

			$this->error('您请求的页面不存在！',U('Home/address/index'));
		}


	}
	
}